package com.example.thread;


import com.alibaba.fastjson.JSON;
import com.example.model.LotteryTicketRes;
import com.example.model.MongoLotteryTicket;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.RecursiveTask;

@Slf4j
public class GetBuyTicketTask extends RecursiveTask<List<LotteryTicketRes>> {
    static final int THRESHOLD = 10;
    List<MongoLotteryTicket> ticketList;
    int start;
    int end;

    public GetBuyTicketTask(List<MongoLotteryTicket> ticketList, int start, int end) {
        this.ticketList = ticketList;
        this.start = start;
        this.end = end;
    }

    @Override
    protected List<LotteryTicketRes> compute() {
        if (end - start <= THRESHOLD) {
            // 如果任务足够小,直接计算:
            List<LotteryTicketRes> lotteryTicketList = new ArrayList<>();
            for (int i = start; i < end; i++) {
                handleRepeat(ticketList,i, lotteryTicketList);
            }
            log.info("compute {}~{} = %{}", start, end);
            return lotteryTicketList;
        }
        // 任务太大,一分为二:
        int middle = (end + start) / 2;
        log.info(String.format("split %d~%d ==> %d~%d, %d~%d", start, end, start, middle, middle, end));
        GetBuyTicketTask subtask1 = new GetBuyTicketTask(this.ticketList, start, middle);
        GetBuyTicketTask subtask2 = new GetBuyTicketTask(this.ticketList, middle, end);
        invokeAll(subtask1, subtask2);
        List<LotteryTicketRes> subresult1 = subtask1.join();
        List<LotteryTicketRes> subresult2 = subtask2.join();
        log.info("compute subresult1 = " + JSON.toJSONString(subresult1) + ",subresult2 = " + JSON.toJSONString(subresult2));
        subresult1.addAll(subresult2);
        log.info("compute result ==> " + JSON.toJSONString(subresult1));
        return subresult1;
    }

    public void handleRepeat(List<MongoLotteryTicket> ticketList, int i, List<LotteryTicketRes> lotteryTicketList){
        try{
            if(i > ticketList.size()-2){
                return;
            }
            long repeatCount = 0;
            int endNo = 1;
            for(int j = ticketList.size()-1; j>=1; j--){
                List<Integer> tempResultList = ticketList.get(j).getResult();
                if(j-i < 0){
                    break;
                }
                if(tempResultList.get(j-i) == 1){
                    repeatCount++;
                }else{
                    // 遇到0，说明没重复了，直接退出
                    endNo = j-i+2;// 记录一下从哪个号码开始重复
                    break;
                }
            }
            if(repeatCount >= 3){
                long buyNo = ticketList.size() -i + 1;

                MongoLotteryTicket buyTicket = ticketList.get(Integer.parseInt(String.valueOf(buyNo-1)));
                LotteryTicketRes lotteryTicketRes = new LotteryTicketRes();
                lotteryTicketRes.setRepeat(repeatCount);
                lotteryTicketRes.setNum(i);
                lotteryTicketRes.setId(buyTicket.getId());
                lotteryTicketRes.setNo(String.valueOf(buyTicket.getNo()));
                lotteryTicketRes.setSerialNo(buyTicket.getId());
                lotteryTicketRes.setContent(String.join(",", buyTicket.getContent()));
                lotteryTicketRes.setStartRepeatNo(Long.valueOf(endNo+""));

                lotteryTicketList.add(lotteryTicketRes);
            }
        }catch (Exception e){
            log.error("handleRepeat exception:", e);
        }
    }
}